package frame.util;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

public class FrameUtil {

    // ----- begin ----- md5 -----------
    public static String md5(String code) {
        if (StringUtils.isEmpty(code)) code = "";
        return DigestUtils.md5DigestAsHex(code.getBytes());
    }

    private static String SALT = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static String randString(int length) {
        Random rand = new Random();
        int len = SALT.length();
        StringBuilder sb = new StringBuilder();
        for (int i=0; i< length; i++) {
            sb.append(SALT.charAt(rand.nextInt(len)));
        }
        return sb.toString();
    }
    // ----- end ------- md5 -----------

    /**
     * 判断integer是否为空
     **/
    public static boolean isEmpty(Integer i) {
        return i == null || i == 0;
    }
    /**
     * 判断Byte是否为空
     **/
    public static boolean isEmpty(Byte i) {
        return i == null || i == 0;
    }
    /**
     * 判断Double是否为空
     **/
    public static boolean isEmpty(Double i) {
        return i == null || i == 0;
    }
    /**
     * 判断Float是否为空
     **/
    public static boolean isEmpty(Float i) {
        return i == null || i == 0;
    }
    /**
     * 判断String是否为空
     **/
    public static boolean isEmpty(String s) {
        return StringUtils.isEmpty(s);
    }
    /**
     * 判断Collection是否为空
     **/
    public static boolean isEmpty(Collection s) {
        return s == null || s.size() == 0;
    }

    /**
     * 判断Map是否为空
     **/
    public static boolean isEmpty(Map s) {
        return s == null || s.size() == 0;
    }

    /**
     * 判断Object[]是否为空
     **/
    public static boolean isEmpty(Object[] s) {
        return s == null || s.length == 0;
    }

    /**
     * 判断byte[]是否为空
     **/
    public static boolean isEmpty(byte[] s) {
        return s == null || s.length == 0;
    }

    /**
     * 判断int[]是否为空
     **/
    public static boolean isEmpty(int[] s) {
        return s == null || s.length == 0;
    }

    /**
     * 判断long[]是否为空
     **/
    public static boolean isEmpty(long[] s) {
        return s == null || s.length == 0;
    }

    /**
     * 判断double[]是否为空
     **/
    public static boolean isEmpty(double[] s) {
        return s == null || s.length == 0;
    }

    /**
     * 判断BigDecimal是否为空
     **/
    public static boolean isEmpty(BigDecimal s) {
        return s == null || s.compareTo(BigDecimal.ZERO) == 0;
    }

    /**
     * 今天 00:00:00 000
     *
     * @return
     */
    public static Date getTodayBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 今天 23:59:59 999
     *
     * @return
     */
    public static Date getTodayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 是否为奇数
     */
    public static boolean odd(int x) {
        return (x & 1) == 1;
    }

    /**
     * 是否为奇数
     */
    public static boolean odd(long x) {
        return (x & 1) == 1;
    }

    /**
     * 是否为偶数
     */
    public static boolean even(int x) {
        return (x & 1) == 0;
    }

    /**
     * 是否为偶数
     */
    public static boolean even(long x) {
        return (x & 1) == 0;
    }

    /**
     * 字符转integer
     * @param str
     * @param def 默认值
     * @return
     */
    public static Integer parseInt(String str, Integer def) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return def;
        }
    }

}
